#!/bin/bash

# 获取环境参数，默认为 test
ENV=${1:-test}

# 如果 dist.zip存在，则删除
if [ -f "dist.zip" ]; then
  rm dist.zip
fi

zip -r -q ./dist.zip dist

# 根据环境设置不同的请求地址
if [ "$ENV" = "prod" ]; then
  # 正式环境
  requestPath="https://www.-----"
  echo "准备上传到正式环境: $requestPath"
else
  # 测试环境
  requestPath="https://www.-----"
  echo "准备上传到测试环境: $requestPath"
fi

# 上传参数
myPlatform="admin"

# 拼接请求地址
URL="$requestPath/api/backend/updateResource"

# 如果是正式环境，添加确认提示
if [ "$ENV" = "prod" ]; then
  read -p "确定要上传到正式环境吗？$requestPath (y/n): " confirm
  if [ "$confirm" != "y" ]; then
    echo "已取消上传"
    exit 1
  fi
fi

# 发送 POST 请求到指定接口
RESPONSE=$(curl -s -X POST -F "zip=@dist.zip" -F "platform=$myPlatform" $URL)

# 打印返回结果
echo "接口返回: $RESPONSE"

# 判断是否包含true
if [[ $RESPONSE =~ "true" ]]; then
  echo "上传成功: $requestPath"
else
  echo "上传失败"
  exit 1
fi

# 删除 dist.zip
rm dist.zip
# 删除 dist
rm -rf dist
